package com.rp.system.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.raptor.pagination.Page2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.rp.common.utils.JsonUtil;
import com.rp.common.vo.PageData;
import com.rp.controller.RpController;
import com.rp.security.extend.MySecurityMetadataSource;
import com.rp.system.po.Resource;
import com.rp.system.service.ResourceService;

@Controller
@RequestMapping("/rp/system/rescMng/")
public class ResourceController extends RpController {

	@Autowired
	private ResourceService resourceService;
	
	@Autowired
	private MySecurityMetadataSource mySecurityMetadataSource;
	
	@RequestMapping("showRescGrid")
	public ModelAndView showRescGrid(){
		
		return forward("/rp/system/rescMng/rescGrid.jsp");
	}
	
	@ResponseBody
	@RequestMapping("getResources")
	public PageData getResources(String moduleId, Resource resource){
		Page2 page = resourceService.getResourceDao().getResourcesByPage(pageParam, moduleId, resource);
		return new PageData(page);
	}
	
	@ResponseBody
	@RequestMapping("getHasResources")
	public List<Resource> getHasResources(String authorityId){
		try {
			return resourceService.getResourceDao().getResourcesByAuthorityId(authorityId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@ResponseBody
	@RequestMapping("getWaitAllotResources")
	public PageData getWaitAllotResources(String moduleId, String authorityId){
		PageData pageData = null;
		try {
			pageData = new PageData(resourceService.getResourceDao().getResourcesByPage2(pageParam, moduleId, authorityId));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return pageData;
	}
	
	@ResponseBody
	@RequestMapping("saveResources")
	public String saveResources(String data){
		try {
			List<Resource> resources = JsonUtil.jsonToList(data, "com.rp.system.po.Resource");
			for (Resource resource : resources) {
				if(resource.getId() == null){
					resourceService.saveResource(resource);
				}else{
					resourceService.updateResource(resource);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
		return SUCCESS;
	}
	
	@ResponseBody
	@RequestMapping("deleteResources")
	public String deleteResources(String data){
		try {
			List<Resource> resources = JsonUtil.getMapper().readValue(data, JsonUtil.getCollectionType(ArrayList.class, Resource.class));
			for (Resource resource : resources) {
				resourceService.deleteResource(resource.getId());
			}
		} catch (JsonParseException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	/**
	 * 重建资源库
	 * @return
	 */
	@ResponseBody
	@RequestMapping("rebuildRescWarehouse")
	public String rebuildRescWarehouse(){
		try {
			mySecurityMetadataSource.init();
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
		return SUCCESS;
	}
	
	
}
